@using System.Reflection

<ErrorView>
    <MudDialogProvider />
    <MudSnackbarProvider />
    <MudPopoverProvider />
    <Router AppAssembly="@typeof(Program).Assembly"
            AdditionalAssemblies="@LazyLoadedAssemblies" OnNavigateAsync="@OnNavigateAsync">
        <Found Context="routeData">
            <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(AdminLayout)">
                <NotAuthorized>
                    <Maanfee.Dashboard.Views.Pages.Authentications.Login />
                </NotAuthorized>
            </AuthorizeRouteView>
        </Found>
        <NotFound>
            <CascadingAuthenticationState>
                <LayoutView Layout="@typeof(NotFoundLayout)">

                </LayoutView>
            </CascadingAuthenticationState>
        </NotFound>
    </Router>
</ErrorView>

@code {

    private List<Assembly> LazyLoadedAssemblies = new();

    private async Task OnNavigateAsync(Microsoft.AspNetCore.Components.Routing.NavigationContext args)
    {
        try
        {
            LazyLoadedAssemblies = new()
            {
                typeof(Maanfee.Dashboard.Examples.Views.Pages._ModulePage).Assembly,
                typeof(Maanfee.Dashboard.Examples.Views.JsInterop.Pages._ModulePage).Assembly,
            };
            LazyLoadedAssemblies.AddRange(LazyLoadedAssemblies);

            await Task.Delay(1);
        }
        catch (Exception ex)
        {
            Snackbar.Add($"{DashboardResource.StringError} : " + ex.Message, Severity.Error);
        }
    }

    protected override async Task OnInitializedAsync()
    {
        IDialogReference dialog = null;

        await LoggingInitializer.InitializedAsync();
        LoggingInitializer.Closed += async () =>
        {
            await Task.Run(() =>
                {
                    DialogParameters DialogParameters = new DialogParameters();

                    dialog = Dialog.Show<DialogServerConnection>(string.Empty, DialogParameters,
                        new DialogOptions()
                            {
                                NoHeader = true,
                                MaxWidth = MaxWidth.Small,
                                FullWidth = true,
                                //FullScreen = true,
                                Position = DialogPosition.Center,
                                CloseOnEscapeKey = true,
                                BackgroundClass = "Dialog-Blur",
                                BackdropClick = false,
                            });
                });
        };
        LoggingInitializer.Reconnected += async () =>
        {
            await Task.Run(() =>
                {
                    if (dialog != null)
                    {
                        dialog.Close();
                    }
                });
        };
    }

}
